#!/bin/bash

#calc: A shell calculator.
#    Copyright (C) 2009  Daniel Gutson
#
#    calc is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    calc is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with calc.  If not, see <http://www.gnu.org/licenses/>.

if [ "$1" ]
then
	F=`mktemp`
	echo "#include <iostream>" > $F
	echo "#include <math.h>" >> $F
	echo "using namespace std;" >> $F
	if [ "$2" ]
	then
		echo "int main() { cout << " $1 >> $F
		echo " << (" >> $F
		echo "$2" >> $F
	else
		echo "int main() { cout << (" >> $F
		echo "$1" >> $F
	fi
	echo ") << endl; return 0; }" >> $F

	g++ -x c++ $F -o $F.out -lm
    R=$?
	if [ -e $F.out ]
	then
		$F.out
        R=$?
		rm $F.out
	fi
	rm $F
    exit $R
else
	echo "calc v 1.2 Copyright (C) Daniel Gutson"
	echo "Usage:"
	echo "	calc [hex] \"formula\""
    exit 1
fi

